use std::{env, fs};
use std::fs::{File, OpenOptions};
use std::io::{stdin, Read, Write};

fn main() {
    let args = env::args();
    for arg in args {
        println!("{}", arg);
    }
    //打印当前路径
    println!("{:?}", env::current_dir().unwrap().as_path());
    //命令行输入
    let mut str_buf = String::new();
    stdin().read_line(&mut str_buf)
        .expect("Failed to read line.");
    println!("Your input line is \n{}", str_buf);
    let text = fs::read_to_string("text.txt").unwrap();
    println!("{}", text);
    let content = fs::read("text.txt").unwrap();
    println!("{:?}", content);
    let mut buffer = [0u8; 5];
    let mut file = fs::File::open("text.txt").unwrap();
    file.read(&mut buffer).unwrap();
    println!("{:?}", buffer);
    file.read(&mut buffer).unwrap();
    println!("{:?}", buffer);

    // 文件写入
    // 文件写入分为一次性写入和流式写入。流式写入需要打开文件，打开方式
    // 有"新建"(create)和"追加"(append)两种。
    let mut file = File::create("text1.txt").unwrap();
    file.write(b"Hello World!").unwrap();


    let mut file = OpenOptions::new()
        .append(true).open("text1.txt");
    file.unwrap().write(b" APPEND WORD");
}
